Python is a programming language that has been under development for over 25 years [1].
Python is an imperative language based on statements. That is, programs in Python consists of lines composed of statements. A statement can be:
In [1]:
1
Out[1]:
In [ ]:
2
In [10]:
-3
Out[10]:
In [2]:
1
2
Out[2]:
In [5]:
3.14
Out[5]:
In [7]:
'apple'
Out[7]:
In [8]:
"apple"
Out[8]:
Notice that the Out might not match exactly the In. In the above example, we used double-quotes but the representation of the string used single-quotes. Python will default to showing representations of values using single-quotes, if it can.
In [21]:
True
Out[21]:
In [22]:
False
Out[22]:
List, and a read-only list (called a tuple
).
In [11]:
[1, 2, 3]
Out[11]:
In [23]:
(1, 2, 3)
Out[23]:
In [24]:
1, 2, 3
Out[24]:
There are two ways to call functions in Python:
Infix operator name:
In [4]:
1 + 2
Out[4]:
In [9]:
abs(-1)
Out[9]:
Evaluating and display result as an Out, versus evaluating and printing result (side-effect).
In [3]:
print(1)
In [12]:
None
In [16]:
def plus(a, b):
return a + b
In [17]:
plus(3, 4)
Out[17]:
In [18]:
def plus(a, b):
a + b
In [19]:
plus(3, 4)
What happened? All functions return something, even if you don't specify it. If you don't specify a return value, then it will default to returning None
.
In [1]:
"a" + 1
Python error messages
TypeError: Can't convert 'int' object to str implicitly
Above the error message is the "traceback" also called the "call stack". This is a representation of the sequence of procedure calls that lead to the error. If the procedure call originated from code from a file, the filename would be listed after the word "File" on each line. If the procedure call originated from a notebook cell, then the word "ipython-input-#-HEX".
In [2]:
1 == 1
Out[2]:
In [4]:
[] is []
Out[4]:
In [6]:
list() is list()
Out[6]:
In [5]:
tuple() is tuple()
Out[5]:
In [7]:
57663463467 is 57663463467
Out[7]:
In [ ]: